# -*- coding:utf-8 -*-
from PySide6.QtWidgets import (QFileDialog, QMessageBox)
from PySide6.QtCore import (QDir)

from app.model.model import AppModel
from app.view.view import AppView

from app.common.global_config import GlobalConfig

from app.features.service.export_service import ExportService

class AppController:
    """
    mvc 框架 controller
    """
    def __init__(self, model:AppModel, view:AppView):
        self.model = model
        self.view = view
        self.view.bind_controller(self)
        # 加载保存的语言设置
        global_config = GlobalConfig.get_instance()
        saved_lang = global_config.get("language", "zh_CN")
        self.view.set_language(saved_lang)
        # 连接single 和 solt，数据变化时更新视图
        self.model.protoc_dir_changed.connect(self.view.update_protoc_path)
        self.model.proto_dir_changed.connect(self.view.update_proto_path)
        self.model.server_dir_changed.connect(self.view.update_server_path)
        self.model.client_dir_changed.connect(self.view.update_client_path)
        self.model.logging_changed.connect(self.view.update_log_view)
        self.model.server_export_type_changed.connect(self.view.update_server_export_type)
        self.model.client_export_type_changed.connect(self.view.update_client_export_type)

    # 响应处理方法
    def handle_protoc_directory_selection(self):
        """
        Protoc命令文件目录按钮响应方法
        """
        directory = QFileDialog.getExistingDirectory(
            self.view,
            "选择Protoc文件目录",
            QDir.homePath(),
            QFileDialog.ShowDirsOnly
        )
        if directory:
            self.model.set_protoc_dir(directory)
            self.add_log(f"Protoc directory selected: {directory}")

    def handle_proto_directory_selection(self):
        """
        导出目录按钮信号响应方法
        """
        directory = QFileDialog.getExistingDirectory(
            self.view,
            "选择Proto文件导出目录",
            QDir.homePath(),
            QFileDialog.ShowDirsOnly
        )
        if directory:
            self.model.set_proto_dir(directory)
            self.add_log(f"Proto directory selected: {directory}")

    def handle_server_directory_selection(self):
        """
        Excel文件目录按钮新号响应方法
        """
        directory = QFileDialog.getExistingDirectory(
            self.view,
            "选择server文件目录",
            QDir.homePath(),
            QFileDialog.ShowDirsOnly
        )
        if directory:
            self.model.set_server_dir(directory)
            self.add_log(f"Server directory selected: {directory}")

    def handle_client_directory_selection(self):
        """
        导出目录按钮信号响应方法
        """
        directory = QFileDialog.getExistingDirectory(
            self.view,
            "选择client文件导出目录",
            QDir.homePath(),
            QFileDialog.ShowDirsOnly
        )
        if directory:
            self.model.set_client_dir(directory)
            self.add_log(f"Client directory selected: {directory}")

    def server_radio_button_changed(self, rb):
        """
        服务端导出类型选择
        """
        if rb.isChecked() == True:
            text = rb.text()
            # 更新model
            self.model.set_server_export_type(text)
            self.add_log(f"Server radio button state changed: {text}")  

    def client_radio_button_changed(self, rb):
        """
        客户端导出类型选择
        """
        if rb.isChecked() == True:
            text = rb.text()
            # 更新model
            self.model.set_client_export_type(text)
            self.add_log(f"Client radio button state changed: {text}")  

    def handle_save(self):
        # 这里保存配置逻辑
        self.model.save_config()

    def add_log(self, message):
        """新增日志记录方法"""
        self.model.set_log_content(message)

    def handle_export(self):
        """
        导出操作
        """
        export_service = ExportService(self)
        export_service.export()

    def save_language_setting(self, lang_code):
        """保存语言设置到配置文件"""
        global_config = GlobalConfig.get_instance()
        global_config.set("language", lang_code)
        
    def show_about_dialog(self):
        """显示关于对话框"""
        lang_data = self.view.language_dict[self.view.current_lang]
        title = lang_data.get("action_about", "About")
        about_text = lang_data.get("about_text", "Protoc2xxx Converter Tool")
        QMessageBox.about(self.view, title, about_text)